﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConfigDialog.Model;
using MoodLaZik.Model;
using System.Xml.Linq;

namespace MoodLaZik.Agent
{
	class Agent
	{
		static public List<Media> Generate(Config _config, MoodLaZik.Model.XmlManipulator _xm)
		{
			if (System.IO.File.Exists("playlist.xml"))
			{
				XDocument xmlDoc = XDocument.Load("playlist.xml");
				XElement root = xmlDoc.Element("Playlist");
				var medias = (from m in root.Elements("Media")
							  select new Media
							  {
								  Path = m.Element("Path").Value,
								  Name = m.Element("Name").Value,
								  Style = m.Element("Style").Value
							  });
				var collection = new List<Media>();
				foreach (Media m in medias)
					collection.Add(m);

				var playlist = new List<Media>();
				/*foreach (Media m in collection)
				{
				}*/


				//FillView();
				//if (!_xm.IsDirPathChanged())
				//	return (null);

				{
					if (!_xm.CheckXmlFiles())
					{
						_xm.CreateMoodvsTypeXMLFile();
						_xm.CreateAgentBrainXMLFile();
						return (null);
					}
				}



				String str = String.Empty;
				_xm.SetHoursFormat(ref str);
				Model.XmlManipulator._infos["HOUR"] = str;

				xmlDoc = XDocument.Load(Model.XmlManipulator._agentBrainXML);
				List<XElement> q = (from c in xmlDoc.Descendants("Sub")
									where c.Attribute("MOOD").Value == Model.XmlManipulator._infos["MOOD"] &&
									c.Attribute("PLACE").Value == Model.XmlManipulator._infos["PLACE"] &&
									c.Attribute("WEATHER").Value == Model.XmlManipulator._infos["WEATHER"] &&
									c.Attribute("ENVIRONMENT").Value == Model.XmlManipulator._infos["ENVIRONMENT"] &&
									c.Attribute("PERSONNALITY").Value == Model.XmlManipulator._infos["PERSONNALITY"] &&
									c.Attribute("HOUR").Value == Model.XmlManipulator._infos["HOUR"]
									select c).ToList();

				if (Model.XmlManipulator._infos.Count == 0) return (null);

				//GenerateVisibility = true;

				String mood = Model.XmlManipulator._infos["MOOD"];
				XDocument moodvstypeXml = XDocument.Load(Model.XmlManipulator._moodvsTypeXML);
				var mfield = (from entry in moodvstypeXml.Elements("MoodvsTypeXMLFile").Elements(mood)
							  select entry.Value);
				if (mfield.Count() != 0)
				{
					/*String str2 = String.Empty;
					if (!_xm.GetDirPathSaved(ref str2))
						str2 = Model.XmlManipulator._dirpath;
					str2 = str2.TrimEnd('\r', '\n');
					var listofsongs = from dir in Directory.EnumerateFiles(str2, "*.mp3")
									  select dir;
					List<string> tmp = new List<string>(mfield.ToList());
					List<string> tmp2 = new List<string>(listofsongs.ToList());*/

					List<string> tmp = new List<string>(mfield.ToList());
					List<string> tmp2 = new List<string>();
					foreach (Media m in collection)
						tmp2.Add(m.Path);
					_xm.AddNewFieldsInAgentBrainXMLFile(ref Model.XmlManipulator._infos, ref tmp2, tmp[0]);

					/*foreach (String s in tmp2)
					{
						String st = Path.GetFileNameWithoutExtension(s);
						PlaylistCollection.Add(new MediaData { SongTitle = st });
					}*/
					//FillView();



					{
						if (Model.XmlManipulator._infos.Count() != 0)
						{
							String s = String.Empty;
							_xm.SetHoursFormat(ref s);
							Model.XmlManipulator._infos["HOUR"] = s;
							List<String> tmp42 = new List<string>();
							_xm.BackUpAgentBrain(Model.XmlManipulator._infos, ref tmp42);
							foreach (String str15 in tmp42)
								playlist.Add(collection.Find((Media m) => { return (str15 + ".mp3" == m.Name); }));
								//PlaylistCollection.Add(new MediaData { SongTitle = str });
							return (playlist);
						}
						return (null);
					}
				}



				return (collection);
			}
			else
				return (null);
		}
	}
}
